
//
//  CircleViewController.swift
//  HoneyBee
//
//  Created by point on 2018/1/24.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit

class CircleViewController: UIViewController , PopupMenuDelegate{
    
    var menuView :PopupMenuView?
    var maskView :UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        setupSearchBar()
        
        setupSegementBar()
        
    }
    
    func setupSearchBar() {
        let searchBar = NavSearchBar(frame: CGRect(x: 0, y: 0, width: kScreenW, height: LayoutCommon.navHeight))
        
        self.view.addSubview(searchBar)
        searchBar.searchTextField.delegate = self
        searchBar.createBtn.addTarget(self, action: #selector(createInstance), for: UIControl.Event.touchUpInside)
    }
    
    
    func setupSegementBar() {
        let titles = ["话题广场","小组广场","我的小组","话题关注"]
        var childVcs = [UIViewController]()
        let style = HYTitleStyle()
        style.selectColor = kColorNavBarBG
        style.normalColor = kColorTextPlacehold
        style.fontSize = 14
        style.titleHeight = 40
        style.scrollLineColor = kColorNavBarBG
        style.isScrollEnable = false
        
        let topicVC = TopicViewController()
        childVcs.append(topicVC)
        
        let groupVC = GroupViewController()
        childVcs.append(groupVC)
        
        let myGroupVC = MyGroupViewController()
        childVcs.append(myGroupVC)
        let topicFocusVC = TopicFocusViewController()
        childVcs.append(topicFocusVC)

        var frame = CGRect(x: 0, y: LayoutCommon.navHeight, width: view.bounds.width, height: view.bounds.height - LayoutCommon.navHeight)
        frame.size.height -= self.tabBarController?.tabBar.height ?? 0;
        let pageView = HYPageView(frame: frame, titles: titles, childVcs: childVcs, parentVc: self , style:style)
        view.addSubview(pageView)
    }
    
    func setupCreateMenu() {
        
        if (menuView != nil) {
            if menuView?.superview != nil {
                return
            }
            view.addSubview(maskView!)
            view.addSubview(menuView!)
            
        } else {
            
            maskView = UIView(frame: self.view.bounds);
            let gesture = UITapGestureRecognizer(target: self, action: #selector(dismissMenu))
            maskView?.addGestureRecognizer(gesture)
            view.addSubview(maskView!)
            
            let topicColor = UIColor(red: 23.0 / 255.0, green: 187.0 / 255.0, blue: 254.0 / 255.0, alpha: 1.0)
            let uploadVideoColor = UIColor(red: 225.0 / 255.0, green: 114.0 / 255.0, blue: 114.0 / 255.0, alpha: 1.0)
            let groupColor = UIColor(red: 255.0 / 255.0, green: 107.0 / 255.0, blue: 106.0 / 255.0, alpha: 1.0)
            
            
            menuView = PopupMenuView(frame: CGRect(x: kScreenW - 93 * kRatio - 10, y: 69 * kRatio, width: 93 * kRatio, height: 89 * kRatio), imageIcons: [["name":kIcon_menu_topic, "color":topicColor], ["name":kIcon_menu_upload, "color":uploadVideoColor], ["name":kIcon_menu_group, "color":groupColor]], title: ["发表话题", "上传视频", "创建群组"])
            menuView?.menuDelegate = self
            view.addSubview(menuView!)
        }
    }
    
    @objc func dismissMenu() {
        if menuView?.superview != nil {
            menuView?.removeFromSuperview()
        }
        
        if maskView?.superview != nil {
            maskView?.removeFromSuperview()
        }

    }
    
 
    @objc func createInstance() {
        
        setupCreateMenu()
        
    }
    
    func selectedIndex(index: Int) {
        if index == 1 {
           let addVideoVc = UploadVideoViewController.init(nibName: "UploadVideoViewController", bundle: nil)
            self.navigationController?.pushViewController(addVideoVc, animated: true)
        }
        if index == 0 {
            
            let addTopic = TopicAddNewViewController.init(nibName: "TopicAddNewViewController", bundle: nil)
            self.navigationController?.pushViewController(addTopic, animated: true)

        }
        
        if index == 2 {
            
            let addGroup = GroupAddNewViewController.init(nibName: "GroupAddNewViewController", bundle: nil)
            self.navigationController?.pushViewController(addGroup, animated: true)
            
        }
        
    }
    
}

extension CircleViewController: UITextFieldDelegate{
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        //        schoolViewModel.keyWord = textField.text!
        //        self.schoolViewModel.publishSubData.onNext(.mpLoadDataUp)
    }
    
}


